--[[
滑轮滚动有问题，改为翻页更加丝滑
]]

local tooltip = create_cdzb_tip()
ac.game:event '游戏-按下esc'(function(_,button,info,anchor)
    tooltip:hide()
end)

class.save_ui1 = extends(class.panel) {
    reload = true,
    new = function (parent,x,y,w,h)
        local panel = class.save_ui1:builder 
        {
            parent = parent,
            x = x,
            y = y,
            w = w,
            h = h,
        }
        return panel
    end,
    init = function (self)
        class.panel.init(self)

        --[装备区域]
        local panel = self:add_panel('',20,20,self.w*0.4-30,self.h - 40)
        panel:add_panel([[UI\存档装备\左侧-底图.tga]],0,0,300,294)
        local sx = panel:add_panel([[UI\存档装备\左侧-装备属性框底.tga]],10,280,282,263)
        self.sxwb = sx:add_text('',20,0,sx.w,sx.h,12,7)

        local w2,h2 = 64,64
        local useds = {
            {'武器',1,2},
            {'胸甲',3,2},
            {'头盔',2,1},
            {'护手',1,4},
            {'靴子',3,4},
            {'宠物',2,3},
        }
        self.useds = {}
        for a,da in ipairs(useds) do
            local name = da[1]
            local x = (da[2] - 1)*100 + 20
            local y = (da[3] - 1)*50 + 20
            local button = panel:add_sitemBtn(x,y,w2,h2)
            button:set_normal_image([[UI\存档装备\左侧-武器头盔胸甲框.tga]])
            button.name = name
            button.title = button:add_text(name,-20,button.h,button.w+40,20,12,5)
            self.useds[a] = button
        end


        --[仓库设置]
        local panel = self:add_panel([[UI\存档装备\右侧黑底.tga]],self.w*0.40,20,self.w*0.6-20,self.h - 30)
        --[翻页]
        local page = self:add_page_area()
        page:set_position(panel.x + (panel.w - page.w)/2,panel.y + panel.h - 20)

        --仓库-菜单
        local menus = {'全部','护手','靴子','头盔','胸甲','武器','宠物','宝石'}
        local mw,mh = 80,40
        local icon1 = [[UI\存档装备\右侧-小标签暗.tga]]
        local icon2 = [[UI\存档装备\右侧-小标签亮.tga]]
        local icon3 = [[UI\存档装备\右侧-小标签选中遮罩.tga]]
        for a,name in ipairs(menus) do
            local x = ((a-1)%5) * (mw + 1) + 20
            local y = (math.ceil(a/5) - 1) * (mh + 5) + 10
            local button = panel:add_button(icon1,x,y,mw,mh)
            button.icon1 = icon1
            button.icon3 = icon3
            button.search = name
            button.text = button:add_text(name,0,0,nil,nil,12,5)
            button.text:set_color(0xff999999)
            button:set_highlight(icon2)
            if a==1 then
                ac.wait(100,function()
                    self:on_button_clicked(button)
                end)
            end
        end
                
        --仓库-按钮
        local item_panel = panel:add_panel('',0,110,panel.w - 30,panel.h - 70)
        local w,h = 64,64
        self.buttons = {}
        for a=1,6 do
            for b=1,6 do
                local x = (b-1)*(w + 4) + 30
                local y = (a-1)*(h + 4)
                local button = item_panel:add_sitemBtn(x,y,w,h)
                button.back = button:add_panel([[UI\存档装备\右侧格子-压暗锁.tga]])
                button.back:hide()
                button.name = '仓库按钮'
                button.index = #self.buttons + 1
                table.insert(self.buttons,button)
            end
        end
        --[强化石]
        local qhs = panel:add_panel([[UI\存档装备\强化石框.tga]],30,panel.h - 35,130,38)
        self.qhs = qhs:add_text('|cff999999强化石:|r0',10,0,nil,nil,10,4)

        return self
    end,
    --[模式]
    set_mode = function(self,mode)
        -- self.mode = mode
        -- self['镶嵌界面']:hide()
        -- self['强化界面']:hide()
        -- self['装备区域']:hide()
        -- if mode=='展示' then
        --     self['装备区域']:show()
        -- elseif mode=='镶嵌' then
        --     self['镶嵌界面']:show()
        -- elseif mode=='强化' then
        --     self['强化界面']:show()
        -- end
    end,
    --[更新]
    update = function(self,flag)
        if flag then
            self.page = 1
            self:set_mode('展示')
        end
        local player = ac.player.self
        local search = self.search or '全部'
        local data = player:get_data('存档装备-显示') or {}
        local used = player:get_data('存档装备-携带') or {}
        local list = data[search] or {}
        local index = (self.page - 1) * #self.buttons
        self.max_page = math.max(math.ceil(#data/#self.buttons),1)
        self.page_text:set_text(('%s/%s'):format(self.page,self.max_page))
        
        for a,button in ipairs(self.buttons) do
            local skill = list[a + index]
            button:set_data(skill)
            button.drag_flag = search=='全部'
        end

        --装备界面
        for a,button in ipairs(self.useds) do
            local skill = used[button.name]
            button:set_data(skill)
        end
        self.sxwb:set_text(player:get_data('存档装备属性文本') or '')
        self.qhs:set_text(('|cff999999强化石:|r%d'):format(player:get_data('强化石') or 0))
        
    end,
        
    --[点击]
    on_button_clicked = function(self,button)
        local search = button.search
        if search then
            if self.last_search then
                self.last_search.text:set_color(0xff999999)
                self.last_search:set_normal_image(self.last_search.icon1)
            end
            self.search = search
            self.last_search = button
            self.last_search.text:set_color(0xff00ff00)
            self.last_search:set_normal_image(self.last_search.icon3)
            self:update()
        end
        local skill = button.skill
        if skill and skill.in_use then
            ac.sync('同步-强化存档装备',skill.id)
        end
        if button.name=='仓库按钮' then
            if japi.GetKeyState(KEY.ALT) then
                ac.sync('同步-分解存档装备',button.skill and button.skill.id)
            end
        end
    end,

    --[进入]
    on_button_mouse_enter = function(self,button)
        local skill = button.skill
        if skill then
            local x,y = button:get_real_position()  
            if skill.in_use then
                local qhwb = format_obj(skill, skill.qhwb)
                tooltip:set_data(skill,qhwb)
            else
                tooltip:set_data(skill)
            end
            tooltip:set_position(x + button.w,y + button.h)
        end
    end,

    --[离开]
    on_button_mouse_leave = function(self,button)
        tooltip:set_data()
    end,
    --[右击装备]
    on_button_right_clicked = function(self,button)
        local skill = button.skill
        if skill then
            if button.name=='仓库按钮' then
                ac.sync('同步-佩戴存档装备',skill.id)
            end
            if skill.in_use then
                ac.sync('同步-取下存档装备',skill.id)
            end
        end
    end,
    --[拖动装备]
    on_button_drag_end = function(self,button,target)
        local index1 = button and button.index
        local index2 = target and target.index
        local skill1 = button and button.skill
        local skill2 = target and target.skill
        if index1 and index2 then
            local index = (self.page - 1) * #self.buttons
            ac.sync('同步-拖动存档装备',index + index1,index + index2)
        elseif skill1 and skill2 and skill2.in_use then
            ac.sync('同步-镶嵌存档宝石',skill2.id,skill1.id)
        end
    end,
    process_event = function(self,name,...)
        local params = {...}
        if name=='强化成功' then
            local id = params[1]
            for a,button in ipairs(self.useds) do
                if button.skill and button.skill.id==id then
                    button:play_animation()
                    break
                end
            end
        elseif name=='镶嵌成功' then
            local id = params[1]
            for a,button in ipairs(self.useds) do
                if button.skill and button.skill.id==id then
                    button:play_animation()
                    break
                end
            end
        end
    end,

}

-- local w,h = 1100,600
-- local ui = class.save_ui1:builder 
-- {
--     x = (1920 - w)/2,
--     y = 150,
--     w = w,
--     h = h,
--     show_flag = false,
--     level = 99,
-- }
